<%@page language="java" import="com.scalar.chiptrack.operations.LotView, 
                                java.util.ArrayList, 
                                java.util.Iterator, 
                                com.scalar.chiptrack.commons.SessionKeys,
                                com.scalar.chiptrack.utils.ConfigManager,
                                java.util.LinkedHashMap,
                                com.scalar.chiptrack.commons.Constants"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<chiptrack:checkLogon sourcePage="/jsp/operations/split_merge_lots.jsp"/>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>

<META content="MSHTML 5.50.4807.2300" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js">
</script>
<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js">
</script>
<SCRIPT language="JavaScript">
    <!-- Added by veeru on 29th June 2004   -->
    var error1 = ""
    var error2 = ""
    <% LinkedHashMap activeLotsMap  = (LinkedHashMap)session.getAttribute("activeLotsMap");
       LotView lotview = null;
    %>
    var quantities = new Array(<%=activeLotsMap.size()%>);
    var lotNumbers = new Array(<%=activeLotsMap.size()%>);
    <%
        java.util.Iterator iterator = activeLotsMap.keySet().iterator();
        for( int i = 0; i < activeLotsMap.size(); i++ )
        {
            lotview = (LotView)activeLotsMap.get(iterator.next());
    %>
    quantities[<%=i%>] = '<%=lotview.getQuantityIn()%>';
    lotNumbers[<%=i%>] = '<%=lotview.getLotID()%>';
    <%  } %>

    var index = 3;
	function openLot()
	{
		  	newWindow = window.open('<%=chipTracContext%>/jsp/engineering/lot_trace_info.jsp','optionsPopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=yes,width=570,height=420,resizable=no');
		  	newWindow.moveTo(130,140);
		  	newWindow.focus();
	}
    function restrictCharacters(element)
    {
        if (event.keyCode < 48 || event.keyCode > 57 || ( element.value == '' && event.keyCode == 48 ) )
	    {
		    event.returnValue = false;
	    }
    }
    function addRow() {
        index++;
        var htmltext = '<div><table border="0" width="90%"><tr><td width="5%">';
        htmltext = htmltext +'<font class="NArialL">'+(index+1)+'.&nbsp;&nbsp;</font></td>';
        htmltext = htmltext +'<td width="15%" >';
        htmltext = htmltext +'<select name="lot" class="txfield" onchange="javascript:getQuantity(this,'+index+')"><option value="" >----- Select -----</option>';
        <%      iterator = activeLotsMap.keySet().iterator();
                lotview = null;
                StringBuffer strBuffer = new StringBuffer();
                while( iterator.hasNext() )
                {
                    lotview    =  (LotView)activeLotsMap.get(iterator.next());
                    strBuffer.append("htmltext = htmltext + '");
                    strBuffer.append("<option value=\"").append(lotview.getLotID()).append("\">");
                    strBuffer.append(lotview.getLotNumber()).append("</option>';\n");
                }
                out.println(strBuffer);
             %>

        htmltext = htmltext +'</select></td>';
        htmltext = htmltext + '<td width="15%"><input type="text" name="maxQuantity" size="15" class="txfield" disabled></td>';
        htmltext = htmltext + '<td width="15%"><input type="text" name="quantity" size="15" class="txfield" onKeyPress="javascript:restrictCharacters(this)" onkeyup="javascript:sumTheQty(this)"></td>';
        htmltext = htmltext + '<td width="10%">&nbsp;</td></tr></table></div>';
        var pa = document.getElementById("font1");
        pa.insertAdjacentHTML('BeforeEnd',htmltext);
    }
    function getQuantity(sourceElement, rowNo)
    {
        var maxQuantity = document.mergeLot["maxQuantity"];
        var lotNumber = document.mergeLot["lot"];
        var quantity = document.mergeLot["quantity"];
        var x = document.mergeLot.mergeQuantity;
        var pa=document.getElementById("message");
        pa.innerHTML =  '&nbsp;';
        error1 = ""
        for( var i = 0; i < ( lotNumber.length - 1 ); i++ )
        {
            for ( var j = i+1 ; j < lotNumber.length ; j++ )
            {
                if( lotNumber[i].value == lotNumber[j].value &&  lotNumber[i].value != '')
                {
                   if( quantity[j].value != '' )
                   {
                        x.value = parseInt(x.value) - parseInt(quantity[j].value);
                   }
                   maxQuantity[j].value = "";
                   quantity[j].value = ""

                   error1 = "<li> This lot is already selected. Please select another lot"
                   pa.innerHTML = error1 ;
                   return;
                }
            }
        }
        for( var i = 0; i < quantities.length; i++ )
        {
            if( sourceElement.value == lotNumbers[i] )
            {
                maxQuantity[rowNo].value = quantities[i];
            }
        }
        if( sourceElement.value == "" )
        {
            maxQuantity[rowNo].value = "";

           if( quantity[rowNo].value != '' )
           {
                x.value = parseInt(x.value) - parseInt(quantity[rowNo].value);
           }
            quantity[rowNo].value = "";

        }
        if(error1 = "")
        {
            pa.innerHTML =  '&nbsp;';
        }
    }

    /*function sumTheQty(){
          var x = document.mergeLot.mergeQuantity;
          x.value = parseInt(document.mergeLot.quantityIn.value);

          if(document.mergeLot.quantity1.value != ""){

                x.value = parseInt(document.mergeLot.quantity1.value) + parseInt(x.value);
          }
          if(document.mergeLot.quantity2.value !=  ""){

                x.value = parseInt(document.mergeLot.quantity2.value) + parseInt(x.value);
          }
          if(document.mergeLot.quantity3.value != ""){

                x.value = parseInt(document.mergeLot.quantity3.value) + parseInt(x.value);
          }
          if(document.mergeLot.quantity4.value != ""){

                x.value = parseInt(document.mergeLot.quantity4.value) + parseInt(x.value);
          }

    }*/

    function sumTheQty( element ){
          var x = document.mergeLot.mergeQuantity;
          x.value = document.mergeLot.quantityIn.value;
          lotNumber = document.mergeLot["lot"];
          maxQuantity = document.mergeLot["maxQuantity"];
          quantity = document.mergeLot["quantity"];
          var pa=document.getElementById("message");
          //pa.innerHTML ="";
          error2 = ""
          for( var i = 0 ; i < quantity.length ; i++ )
          {
                if ( element == quantity[i] )
                {
                    if( lotNumber[i].value == "" && quantity[i].value != "")
                    {
                        error2 = "<li>  Please select Lot Number";
                        quantity[i].value ='';
                        pa.innerHTML = error1 + error2;
                        return;
                    }
                    for( var j = 0; j < quantities.length; j++ )
                    {
                        if( lotNumber[i].value == lotNumbers[j] )
                        {
                            if( quantities[j] - quantity[i].value >= 0 )
                            {
                                maxQuantity[i].value = quantities[j] - quantity[i].value
                            }
                            else
                            {
                                error2 = "<li> Quantity requested should not be greater than Available Quantity ";
                                quantity[i].value ='';
                                pa.innerHTML = error1+error2;
                                return;
                            }
                        }
                    }
                }
                if( quantity[i].value != "")
                {
                    x.value = parseInt(quantity[i].value) + parseInt(x.value);
                }
          }
          if( error1 == '' && error2 == '')
          {
                pa.innerHTML = '&nbsp;';
          }
          else
          {
                pa.innerHTML = error1+error2;
          }
    }
    <!-- End of Added by veeru -->
    function submitPage(action)
    {
        var pa       = document.getElementById("message");
        var quantity = document.mergeLot["quantity"];
        var lot      = document.mergeLot["lot"];
        lotSelected  = false;
        for( var i = 0 ; i < quantity.length ; i++ )
        {
            if( !isInteger( quantity[i].value ) || quantity[i].value == "0"  )
            {
                error2 = "<li>  Quantity should be positive integer";
                pa.innerHTML = error2 ;
            }
            if( lot[i].value != "" && quantity[i].value == "")
            {
                error2 = "<li>  Please enter Quantity";
                lotSelected = true;
                break;
            }
            if( lot[i].value != "" && quantity[i].value != "" )
            {
                lotSelected = true;
            }
        }
        if( lotSelected == false )
        {
           error2 = "<li>  Select a Lot Number and enter Requested Quantity for at least one Lot";
        }
        if( error1 != "" ||  error2 != "" )
        {
            pa.innerHTML = error1 + error2;
            return;
        }
        else
        {
            document.mergeLot.totalMergeQty.value =  document.mergeLot.mergeQuantity.value;
            document.mergeLot.lotaction.value = action;
            document.mergeLot.submit();
        }
    }


</SCRIPT>

<!-- Top Header Start-->

<%@include file="/jsp/includes/header.jsp"  %>

<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
	<td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

<!-- Side Navigation start -->

<%@include file="/jsp/operations/operations_menu.jsp" %>

</TD>

<!-- end of the side navigations -->
<TD bgcolor="#ffffff">
<IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
<TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
<TD valign="top" width="100%">

<IMG height=1 src="images/clear.gif" width=500 border=0>
<BR>


<!-- from here your body starts -->
<html:form name="mergeLot"  type="com.scalar.chiptrack.ChipTrackActionForm" action="/mergeLot.do">
<center><span class="NArialW">Merge Lot</span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10>
	<%--<center>
	<strong><font face="Verdana" size="-5" align="center" color="#ff0000" id="message"></font>
	</center> --%>
<%
	String message = (String)request.getAttribute("errorMessage");
   	if(message==null)
   		message="";
%>
<%
 	if(!message.equals("")){
%>
<%--<p align ="center">
<strong><font face="Verdana" size="-5" align="center" color="#ff0000"><b><%=message%></b></font>
</p> --%>
    <SCRIPT language="JavaScript1.2">
    	var pa=document.getElementById("message");
        alert("<%=message%>");
    	pa.innerHTML = "<%= message%>";
    </SCRIPT>
<%
 	}
 %>


<%
		LotView lview = (LotView)request.getAttribute("lview");
		String from = (String)request.getAttribute("from");
		session.setAttribute(SessionKeys.LOT_VIEW, lview);

%>
<table border="0" width="90%" align=right>
<tr><td  colspan="3" align="left" class='NArialL'><FONT id="message" styleClass="NArialL" color='FF0000'>&nbsp;</FONT></TD></TR>
<% if ( !user.isActionAllowed ( Functionality.SPLIT_MERGE_LOTS, User.USER_ACTION_MODIFY  )  ) { %>
<tr><td colspan='3'>
<font class='NArialS' color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b>
</font></td></tr>
<% } %>
<tr><td colspan=3>

<%--<%@include file="/jsp/engineering/lot_info.jsp" %>--%>
  </td>
  </tr>
  <tr><td width="3%" colspan=6><img src="images/spacer.gif" height=10></td></tr>
<tr><td colspan=6>
    	    	<table border="1" width="80%" bordercolor="white">
			<tr>
             <td width="5%"><font class="NArialL">&nbsp;</font></td>
			 <td width="15%"><font class="NArialL">&nbsp;&nbsp;&nbsp; Lot No</font></td>
			 <td width="15%"><font class="NArialL">Max Quantity</font></td>
             <td width="15%"><font class="NArialL">Req Quantity</font></td>
			 <td width="10%"><font class="NArialL">Lot Number To merge</font></td>
			</tr>
            <tr>
             <td width="5%"><font class="NArialL">1.&nbsp;&nbsp;</font></td>
			 <td width="15%"><%-- <input type="text" name="lot1" size="20" class="txfield"> --%>
             <select name="lot" class="txfield" onchange="javascript:getQuantity(this,0)" >
                             <option value="" >----- Select -----</option>
                             <%
                                iterator = activeLotsMap.keySet().iterator();
                                while( iterator.hasNext() )
                                {
                                    lotview = (LotView)activeLotsMap.get(iterator.next());
                                    %>
                                        <option value="<%=lotview.getLotID()%>"><%=lotview.getLotNumber()%></option>
                                    <%
                                }
                             %>
             </select>
             </td>
             <td width="15%"><input type="text" name="maxQuantity" size="15" class="txfield" disabled></td>
			 <td width="15%"><input type="text" name="quantity" size="15" class="txfield" onKeyPress="javascript:restrictCharacters(this)" onkeyup="javascript:sumTheQty(this)"></td>
			 <td width="10%"><input type="text" name="mergeLot" size="20" class="txfield" value="<%= lview.getLotNumber()%>" disabled=true></td>
			</tr>
			<tr>
             <td width="5%"><font class="NArialL">2.&nbsp;&nbsp;</font></td>
			 <td width="15%">
             <select name="lot" class="txfield" onchange="javascript:getQuantity(this,1)" >
                             <option value="" >----- Select -----</option>
                             <%
                                iterator = activeLotsMap.keySet().iterator();
                                while( iterator.hasNext() )
                                {
                                    lotview = (LotView)activeLotsMap.get(iterator.next());
                                    %>
                                        <option value="<%=lotview.getLotID()%>"><%=lotview.getLotNumber()%></option>
                                    <%
                                }
                             %>
             </select></td>
             <td width="15%"><input type="text" name="maxQuantity" size="15" class="txfield" disabled></td>
			 <td width="15%"><input type="text" name="quantity" size="15" class="txfield" onKeyPress="javascript:restrictCharacters(this)" onkeyup="javascript:sumTheQty(this)"></td>
			 <td width="10%"><input type="text" name="mergeQuantity" size="20" class="txfield" value="<%= lview.getQuantityIn()%>" disabled=true></td>
			</tr>
			<tr>
             <td width="5%"><font class="NArialL">3.&nbsp;&nbsp;</font></td>
			 <td width="15%">
             <select name="lot" class="txfield" onchange="javascript:getQuantity(this,2)">
                             <option value="" >----- Select -----</option>
                             <%
                                iterator = activeLotsMap.keySet().iterator();
                                while( iterator.hasNext() )
                                {
                                    lotview = (LotView)activeLotsMap.get(iterator.next());
                                    %>
                                        <option value="<%=lotview.getLotID()%>"><%=lotview.getLotNumber()%></option>
                                    <%
                                }
                             %>
             </select></td>
             <td width="15%"><input type="text" name="maxQuantity" size="15" class="txfield" disabled></td>
			 <td width="15%"><input type="text" name="quantity" size="15" class="txfield" onKeyPress="javascript:restrictCharacters(this)" onkeyup="javascript:sumTheQty(this)"></td>
			 <td width="10%">&nbsp;</td>
			</tr>
            <tr>
             <td width="5%"><font class="NArialL" >4.&nbsp;&nbsp;</font></td>
			 <td width="15%">
             <select name="lot" class="txfield" onchange="javascript:getQuantity(this,3)">
                                 <option value="" >----- Select -----</option>
                             <%
                                iterator = activeLotsMap.keySet().iterator();
                                while( iterator.hasNext() )
                                {
                                    lotview = (LotView)activeLotsMap.get(iterator.next());
                                    %>
                                        <option value="<%=lotview.getLotID()%>"><%=lotview.getLotNumber()%></option>
                                    <%
                                }
                             %>
             </select></td>
             <td width="15%"><input type="text" name="maxQuantity" size="15" class="txfield" disabled></td>
			 <td width="15%"><input type="text" name="quantity" size="15" class="txfield" onKeyPress="javascript:restrictCharacters(this)" onkeyup="javascript:sumTheQty(this)"></td>
			 <td width="10%">&nbsp;</td>
			</tr>
            <tr><td colspan='5'><FONT id="font1"></FONT></TD></TR>
			<tr>
              <td width="5%">&nbsp;</td>
              <td width="15%">&nbsp;</td>
              <td width="15%">&nbsp;</td>
               <td width="15%">&nbsp;</td>
			  <td width="10%" align=left><a href="javascript:addRow();"> <img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif"onmouseover="window.status='Click to Add More Lots';return true" onmouseout="window.status='';return true" title="Click here to Add More Lots"></a></td>
			</tr>
		    	</table>

    </td></tr>

	<tr><td colspan=6><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
   <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
   </tr>
   <tr>
      <td colspan=5 align=center>
      <% String strFrom = "javascript:submitPage('"+from+"');"; %>
      <chiptrack:restrictedLink functionality='<%=Functionality.SPLIT_MERGE_LOTS%>' action='<%=User.USER_ACTION_MODIFY%>' href='<%=strFrom%>' onmouseover="window.status='Click to Save Merges';return true" onmouseout="window.status='';return true">
        <img border="0" src="<%=chipTracContext%>/jsp/includes/images/submit.gif" width="53" height="17" title="Click here to Save Merges">
      </chiptrack:restrictedLink>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <%--<img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif">--%>
      <a href="javascript:document.mergeLot.reset();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif"  onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"  width="53" height="17"></a>
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <a href="javascript:history.back();"><img border="0" width="39" height="17" src="<%=chipTracContext%>/jsp/includes/images/back.gif"  onmouseover="window.status='Click to go back';return true" onmouseout="window.status='';return true" title="Click here to go back"  width="53" height="17"></a>

      </td>
    </tr>
  </table>

<input type="hidden" name="lotaction" value="">
<input type="hidden" name="quantityIn" value="<%= lview.getQuantityIn()%>">
<input type="hidden" name="totalMergeQty" value="">

</html:form>
<!-- till here -->
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>
<!-- Footer starts here -->

<%@include file="/jsp/includes/footer.jsp" %>

<!-- footer end here -->


</BODY>
</HTML>
